//
//  MusicTableViewController.m
//  UI_homework_22_QQ音乐界面和阿里星球界面
//
//  Created by li cheng on 16/8/13.
//  Copyright © 2016年 li cheng. All rights reserved.
//

#import "MusicTableViewController.h"
#import "TableViewCellOfChannelAccess.h"
#import "TableViewCellOfSongRecommend.h"
#import "TableViewCellMiddleCommon.h"
#import "ExclusiveTableViewCell.h"
#import "Album.h"
#import "WH_CycleImage.h"
#import "RootModel.h"
#import "DataModel.h"
#import "ActionModel.h"
#import "SecondDataModel.h"
#import "SecondActionModel.h"
@interface MusicTableViewController ()
@property (nonatomic, strong) RootModel *rootModel;
@property (nonatomic, strong) NSMutableArray *mArray;
@end

@implementation MusicTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
    
    [self hangdleData];
    self.edgesForExtendedLayout = NO;
    self.automaticallyAdjustsScrollViewInsets = NO;
    [self.tableView registerClass:[TableViewCellOfChannelAccess class] forCellReuseIdentifier:@"TableViewCellOfChannelAccess"];
    [self.tableView registerClass:[TableViewCellOfSongRecommend class] forCellReuseIdentifier:@"TableViewCellOfSongRecommend"];
    [self.tableView registerClass:[TableViewCellMiddleCommon class] forCellReuseIdentifier:@"TableViewCellMiddleCommon"];
    [self.tableView registerClass:[ExclusiveTableViewCell class] forCellReuseIdentifier:@"ExclusiveTableViewCell"];

}
#pragma mark - 数据解析
- (void)hangdleData{
    NSURLSession *urlSession = [NSURLSession sharedSession];
    NSURL *url = [NSURL URLWithString:@"http://api.dongting.com/frontpage/frontpage?location=0&version=1470998222737&app=ttpod&v=v9.0.4.2016070616&user_id=0&mid=iPhone8%2C1&f=f320&s=s310&imsi=&hid=&splus=10.0&active=1&net=2&openudid=4bbd8925c4a3991ee51c9cd8b9a8dc5961820764&idfa=2BBAE4F7-2E3B-41AA-AE0C-F91FB2BDC279&utdid=V62kivWPJBYDABK%2FL8gCUnDI&alf=(null)&bundle_id=com.ttpod.music&latitude=(null)&longtitude=(null)&access_token=(null)&vc=9000400"];
    NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:url];
    urlRequest.HTTPMethod = @"GET";
    NSURLSessionDataTask *dataTask = [urlSession dataTaskWithURL:url completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
       dispatch_sync(dispatch_get_main_queue(), ^{
           NSError *error = nil;
           NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
           self.rootModel = [RootModel modelWithDictionary:dic];
           _mArray = [NSMutableArray array];
           for (SecondDataModel *secondData in _rootModel.data[0].data) {
               [_mArray addObject:secondData.picUrl];
           }
           [self.tableView reloadData];
       });
    }];
    [dataTask resume];
    
}
#pragma mark - 分区数量
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    WH_CycleImage *cycleIamge = [[WH_CycleImage alloc] initWithFrame:CGRectMake(0, 0, 320, 200)];
    cycleIamge.array = _mArray;
    self.tableView.tableHeaderView = cycleIamge;
    
    return _rootModel.data.count;
}
#pragma mark - 得写这个方法,下面的分区头视图才会有
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        return nil;
    } else {
        return @"fdasf";
    }
}
#pragma mark 给分区头视图一个 UIView
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        return nil;
    } else {
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 40)];
        view.backgroundColor = [UIColor purpleColor];
        CGFloat widthOfLabel = tableView.bounds.size.width * 2 / 3;
        CGFloat heightOfLabel = view.bounds.size.height;
        UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(tableView.bounds.size.width / 6, 0, widthOfLabel, heightOfLabel)];
        [view addSubview:label];
        label.text = @"daffdsafsdfasd";
        label.textAlignment = 1;
        UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
        CGFloat widthOfButton = self.tableView.bounds.size.width / 6;
        button.frame = CGRectMake(self.tableView.bounds.size.width * 5 / 6
, 0, widthOfButton, view.frame.size.height);
        [button setTitle:@"更多>" forState:UIControlStateNormal];
        [view addSubview:button];
        return view;
    }
}
#pragma mark - 不悬浮
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    if (scrollView == self.tableView)
    {
        CGFloat sectionHeaderHeight = 40;
        if (scrollView.contentOffset.y<=sectionHeaderHeight&&scrollView.contentOffset.y>=0) {
            scrollView.contentInset = UIEdgeInsetsMake(-scrollView.contentOffset.y, 0, 0, 0);
        } else if (scrollView.contentOffset.y>=sectionHeaderHeight) {
            scrollView.contentInset = UIEdgeInsetsMake(-sectionHeaderHeight, 0, 0, 0);
        }
    }
}
#pragma mark - 每一个 row 的数量
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section >= 0 && section <= 4) {
        return 1;
    } else {
        return 4;
    }
}
#pragma mark - cell 赋值
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    switch (indexPath.section) {
        case 0:
        {
            TableViewCellOfChannelAccess *cell = [tableView dequeueReusableCellWithIdentifier:@"TableViewCellOfChannelAccess" forIndexPath:indexPath];
            cell.backgroundColor = [UIColor yellowColor];
            return cell;
        }
            break;
        case 1:
        {
            TableViewCellOfSongRecommend *cell = [tableView dequeueReusableCellWithIdentifier:@"TableViewCellOfSongRecommend" forIndexPath:indexPath];
            
            cell.backgroundColor = [UIColor yellowColor];
            return cell;
        }
            case 2:
        {
            TableViewCellMiddleCommon *cell = [tableView dequeueReusableCellWithIdentifier:@"TableViewCellMiddleCommon" forIndexPath:indexPath];
            cell.backgroundColor = [UIColor magentaColor];
            return cell;
        }
        case 3:
        {
            TableViewCellMiddleCommon *cell = [tableView dequeueReusableCellWithIdentifier:@"TableViewCellMiddleCommon" forIndexPath:indexPath];
            cell.backgroundColor = [UIColor magentaColor];
            return cell;
        }
        case 4:
        {
            TableViewCellMiddleCommon *cell = [tableView dequeueReusableCellWithIdentifier:@"TableViewCellMiddleCommon" forIndexPath:indexPath];
            cell.backgroundColor = [UIColor magentaColor];
            return cell;
        }
        case 5:
        {
            TableViewCellMiddleCommon *cell = [tableView dequeueReusableCellWithIdentifier:@"TableViewCellMiddleCommon" forIndexPath:indexPath];
            cell.backgroundColor = [UIColor magentaColor];
            return cell;
        }
        default:
        {
            ExclusiveTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ExclusiveTableViewCell" forIndexPath:indexPath];
            cell.backgroundColor = [UIColor yellowColor];
            return cell;
        }
            break;
    }
   
}
#pragma mark - 每个 row 的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    switch (indexPath.section) {
        case 0:
        {
            return 55;
        }
            break;
        case 1:
        {
            return 300;
        }
            break;
        case 2:
        {
            return [UIScreen mainScreen].bounds.size.height / 4;
        }
            break;
        case 3:
        {
            return [UIScreen mainScreen].bounds.size.height / 4;
        }
            break;
        case 4:
        {
            return [UIScreen mainScreen].bounds.size.height / 4;
        }
            break;
        default:
        {
            return 44;
        }
            break;
    }
}

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
@end
